package com.hubu.list;
import com.hubu.list.util.Printer;
public class DoubleCircleList<T> extends AbstractList<T>  implements List<T> ,Queue<T>,Stack<T> {
    private Node<T> head=null;
    private Node<T> tail=null;
    public DoubleCircleList(Printer printer) {
        super(printer);
    }




    @Override
    public void clear() {

    }
    @Override
    public boolean add(T value) {
        addLast(value);
        return true;
    }




    //从头部删除

    public T removeFirst(){
        if(head==null){
            throw new RuntimeException("head is null");
        }


        //创建一个临时引用指向head 过来该函数空间就会被回收



        T value=head.value;
        Node<T> t=head;



        head=head.next;





        head.prev=tail;
        tail.next=head;


        if(head==tail){
            head=tail=null;
        }
        //help gc
        t.prev=null;
        t.next=null;


        size--;

        return value;
    }


    /**
     *
     * 从尾部删除
     * @return
     */
    public T removeLast(){
        return null;
    }


    /**
     *
     *
     * 从尾部添加
     * @param value
     */
    public void addLast(T value){
        if(tail==null){
            head=tail=new Node<>(value,null,null);
            head.prev=tail;
            tail.next=head;
            size++;
            return;
        }
        Node<T> newNode=new Node<>(value,null,null);
        newNode.prev=tail;
        tail.next=newNode;
        newNode.next=head;
        tail=newNode;
        head.prev=tail;
        size++;
    }

    /**
     *
     *
     * 从头部添加
     * @param value
     */
    public void addFirst(T value){
        if(tail==null){
            head=tail=new Node<>(value,null,null);
            head.prev=tail;
            tail.next=head;
            size++;
            return ;
        }
        Node<T> newNode=new Node<>(value,null,null);
        newNode.next=head;
        head.prev=newNode;
        newNode.prev=tail;
        head=newNode;
        tail.next=head;
    }
    @Override
    public T get(int index) {
        return null;
    }

    @Override
    public T set(int index, T newValue) {
        return null;
    }

    @Override
    public boolean add(int index, T value) {
        return false;
    }

    @Override
    public T remove(int index) {
        return null;
    }

    @Override
    public void offer(T value) {

    }

    @Override
    public T poll() {
        return null;
    }


    @Override
    public void push(T value) {
        addLast(value);
    }

    @Override
    public T pop() {
        return null;
    }

    @Override
    public T top() {
        return null;
    }
    static class Node<T>{
        T value;
        Node<T> prev;
        Node<T> next;

        public Node(T value,Node<T> prev,Node<T> next){
            this.value=value;

            this.prev=prev;

            this.next=next;
        }
    }
}
